'use strict';

/**
 * @description 房屋表模型
 * */
module.exports = app => {
  const { STRING, TEXT, INTEGER, FLOAT } = app.Sequelize;

  const House = app.model.define('house', {
    name: {
      type: STRING(20),
      allowNull: false,
      comment: '房屋名',
    },
    address: {
      type: TEXT,
      allowNull: false,
      comment: '房屋地址',
    },
    rent: {
      type: INTEGER,
      allowNull: false,
      comment: '租金',
    },
    lon: {
      type: FLOAT,
      allowNull: false,
      comment: '经度',
    },
    lat: {
      type: FLOAT,
      allowNull: false,
      comment: '纬度',
    },
    areaId: {
      type: INTEGER,
      allowNull: false,
      comment: '所在地区的id',
    },
    oid: {
      type: INTEGER,
      allowNull: false,
      comment: '房东id',
    },
    description: {
      type: TEXT,
      allowNull: false,
      comment: '房屋描述',
    },
    views: {
      type: INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: '房屋浏览量',
    },
    status: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: '房屋状态 1-待审核 2-审核不通过 3-下架 4-上架 5-已出租',
    },
  });

  House.associate = () => {
    app.model.House.belongsTo(app.model.User, {
      foreignKey: 'oid',
      as: 'Owner',
    });
    app.model.House.belongsTo(app.model.Area, {
      foreignKey: 'areaId',
    });
    app.model.House.hasOne(app.model.HouseInfo, {
      foreignKey: 'houseId',
    });
    app.model.House.hasMany(app.model.Reply, {
      foreignKey: 'houseId',
    });
    app.model.House.hasMany(app.model.Lease, {
      foreignKey: 'houseId',
    });
  };

  return House;
};
